using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using TrackMe.Apps.Views;
using TrackMe.Dto;

namespace TrackMe.Apps.VM
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class TaskEditVM : BasicViewModel<ITaskEditView>
	{
		#pragma warning disable 0649
		[Import]
        private IDatabase database;
		#pragma warning restore 0649

		public ICommand SaveCmd { get; private set; }

        public IEditTask Model { get; set; }

        [ImportingConstructor]
        public TaskEditVM(ITaskEditView view): base(view)
        {
            SaveCmd = new DelegateCommand<string>(OnSave);
            RaisePropertyChanged(() => SaveCmd);
        }

        private void OnSave(string s)
        {
            if (Model.Name.Trim().Length == 0)
                throw new Exception("Name should not be empty");

            Model.Id = database.SaveTask(Model);
            ViewCore.Close();
        }

    }
}